<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use App\Model\ShopTypeModel;
use App\Model\ShopBrandModel;
use App\Model\ShopLabelModel;
use App\Model\StoreGoodModel;
//后台模块表

class ShopGoodModel extends Model
{
	//定义一个表名
	protected $table = 'shop_good';
	//定义主键
	protected $primaryKey = 'id';
	protected $guarded = [];

	//权限
	public function listAll()
	{
		$list = $this->orderBy('id', 'desc')->get();
		return $list;
	}


	function getLabelAttribute($value)
	{	

		$labelArr=explode(',',rtrim($this->labels,','));
		if($this->labels){
			$str='';
			foreach($labelArr as $v){
				$str.=ShopLabelModel::where('id',$v)->value('name').",";

			}
		}else{
			$str = '无';
		}

		return $str;

	}

	//启用禁用
	public function updateState($id,$state)
	{
		$res = $this->find($id);
		if($state == 1){
			$msg = '更改商品【'.$res['name'].'】(id:'.$res['id'].')状态为【上架】';
		}
		if($state == 2){
			$msg = '更改商品【'.$res['name'].'】(id:'.$res['id'].')状态为【下架】';
		}

		$arr = $this->where("id",$id)->update(['state' => $state]);
		if($arr){
			//添加日志
			AdminLogsModel::addLog($msg);
			$returnArr['code'] = 1;
			$returnArr['msg'] = "修改成功";
		}else{
			$returnArr['code'] = 2;
			$returnArr['msg'] = "修改失败";
		}
		return $returnArr;
	}

	//启用禁用
	public function updateWish($id,$wish)
	{
		$res = $this->find($id);
		if($wish == 1){
			$msg = '更改商品【'.$res['name'].'】(id:'.$res['id'].')心愿单开关为【开启】';
		}
		if($wish == 2){
			$msg = '更改商品【'.$res['name'].'】(id:'.$res['id'].')心愿单开关为【关闭】';
		}

		$arr = $this->where("id",$id)->update(['is_wish' => $wish]);
		if($arr){
			//添加日志
			AdminLogsModel::addLog($msg);
			$returnArr['code'] = 1;
			$returnArr['msg'] = "修改成功";
		}else{
			$returnArr['code'] = 2;
			$returnArr['msg'] = "修改失败";
		}
		return $returnArr;
	}

    //获取图片全路径
    function getPicsnameAttribute()
    {
    	if($this->oss_pic){
    		return $this->oss_pic;
		}else{
			$lujing = 'http://'.$_SERVER["HTTP_HOST"].$this->picpath.$this->picname;
			return $lujing;
		}

    }

    //商品加销量
    public static function min($product_id,$num)
    {
        if($num<0){
//            return ['code'=>0,'msg'=>retStrInfo(202)];//数据错误
            throw new \Exception(retStrInfo(202));//数据错误
        }
        $product = self::where('id',$product_id)->first();
        if($product['state'] != 1){
//            return ['code'=>0,'msg'=>retStrInfo(200)];//该商品已下架
            throw new \Exception(retStrInfo(200));//该商品已下架
        }
        //加销量
        self::where('id',$product_id)->increment('sale_num',$num);
		$store_sum = StoreGoodModel::where('gid',$product_id)->sum('num');
		if($product->is_attr != 1){
			if($num - $store_sum > 0){
				$due_num = $num - $store_sum;
				//减库存
				self::where('id',$product_id)->decrement('num',$due_num);
			}
		}
    }

    //查询推荐专区的商品
	static function getSell(){
		$data = [];
		$list = ShopGoodModel::where('is_sell',1)->where('state',1)->get();
		foreach ($list as $k=>$v){
			//商品ID
			$data[$k]['id'] = $v['id'];
			//商品名
			$data[$k]['name'] = $v['name'];
			//销售价
			$data[$k]['sell_price'] = number_format($v['sell_price'],2,".","");
			//商品图片
			$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];
			//已入库
			$data[$k]['store_num'] = StoreGoodModel::where('gid',$v['id'])->sum('num');
			//销量
			$data[$k]['sell_num'] = $v['sale_num'];
			$label_res = [];
				$label = explode(',',$v['labels']);
				foreach($label as $key=>$val){
					$label_info = ShopLabelModel::where('id',$val)->first();
					if($label_info){
						$label_res[] = $label_info['color'];
					}
				}
				$data[$k]['label'] = array_values($label_res);
				//专柜价
				$data[$k]['del_coin'] = $v['del_coin'];
		}
		return $data;
	}

	//查询销售价
	function getSpriceAttribute(){
		$price = AddShop::where('id',$this->gid)->value('new_sell_price');
		return $price;
	}

	//查询专柜价
	function getDelCoinAttribute(){
		$price = AddShop::where('id',$this->gid)->value('del_coin');
		return $price;
	}


}
